/**
 * Copyright 2020-9999 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package su.transaction.service;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import cn.hutool.extra.spring.SpringUtil;
import su.transaction.dao.OrderDao;
import su.transaction.dao.ProductDao;
import su.transaction.entity.Order;


@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProductService productService;

    @Transactional(propagation = Propagation.REQUIRED)
    public void submitOrder() {
        //生成订单
        Order order = new Order();
        long number = Math.abs(new Random().nextInt(500));
        order.setId(number);
        order.setOrderNo("order_" + number);
        orderDao.saveOrder(order);
        //减库存
        System.out.println("this: " + this.getClass());
        this.updateProductStockCountById(1, 1L);
        int i = 1 / 0;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public void submitOrder2() {
        //生成订单
        Order order = new Order();
        long number = Math.abs(new Random().nextInt(500));
        order.setId(number);
        order.setOrderNo("order_" + number);
        orderDao.saveOrder(order);
        //减库存
        System.out.println("this: " + this.getClass());
        PlatformTransactionManager transactionManager = null;
        TransactionStatus tran = null;
        try {

            // 手动开启事务
            transactionManager = (PlatformTransactionManager) SpringUtil.getBean("transactionManager");
            DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
            tran = transactionManager.getTransaction(transactionDefinition);
            this.updateProductStockCountById(1, 1L);
            int i = 1 / 0;
        } catch (Exception e) {
            System.out.println("start rollback...");
            transactionManager.rollback(tran);
            throw new RuntimeException(e);
        }
        transactionManager.commit(tran);

    }

    public void updateProductStockCountById(Integer stockCount, Long id) {
        productDao.updateProductStockCountById(stockCount, id);
    }
}
